<template>
   <view class="container">
      <!-- 下拉刷新组件 -->
      <scroll-view scroll-y="true" class="scroll-view" refresher-enabled :refresher-triggered="refreshing"
         @refresherrefresh="onRefresh" @scrolltolower="loadMore">
         <!-- 列表内容 -->
         <view class="list">
            <view class="item" v-for="(item, index) in intentionList" :key="index">
               <view class="item-content" @click="handleToSpotDetail(item)">
                  <view class="item-row">
                     <text class="item-label">联系电话:</text>
                     <text class="item-value">{{ item.phone }}</text>
                  </view>
                  <view class="item-row">
                     <text class="item-label">月租价格:</text>
                     <text class="item-value">{{ item.pricePerMonth }}元/月</text>
                  </view>
                  <view class="item-row">
                     <text class="item-label">最短租期:</text>
                     <text class="item-value">{{ item.minRentalPeriod }}个月</text>
                  </view>
                  <view class="item-row">
                     <text class="item-label">时间:</text>
                     <text class="item-value">{{ item.createTime }}</text>
                  </view>
               </view>
            </view>

            <!-- 加载更多提示 -->
            <view class="load-more">
               <text v-if="loading">加载中...</text>
               <text v-else-if="noMore">没有更多数据了</text>
               <text v-else>上拉加载更多</text>
            </view>
         </view>
      </scroll-view>
   </view>
</template>

<script>
   import {
      listSendApply,
      listReceivedApply
   } from "@/api/parking/apply";

   export default {
      data() {
         return {
            intentionList: [],
            page: 1,
            pageSize: 10,
            loading: false,
            noMore: false,
            refreshing: false,
            // 意向类型： send-发出的，received-收到的
            applyType: '',
         };
      },
      onLoad(option) {
         this.applyType = option.applyType;
         uni.setNavigationBarTitle({
            title: this.applyType == 'send' ? '发出的意向' : '收到的意向'
         })
      },
      onShow() {
         this.onRefresh();
      },
      // onPullDownRefresh() {
      //    this.page = 1;
      //    this.loadData();
      // },
      methods: {
         // 加载数据
         async loadData() {
            if (this.loading || this.noMore) return;
            this.loading = true;
            try {
               var res = null;
               if ('send' === this.applyType) {
                  res = await listSendApply({
                     pageNum: this.page,
                     pageSize: this.pageSize
                  });
               } else {
                  res = await listReceivedApply({
                     pageNum: this.page,
                     pageSize: this.pageSize
                  });
               }


               const newData = res.rows || [];

               if (this.page === 1) {
                  this.intentionList = newData;
               } else {
                  this.intentionList = [...this.intentionList, ...newData];
               }

               // 判断是否还有更多数据
               if (newData.length < this.pageSize) {
                  this.noMore = true;
               } else {
                  this.page++;
               }
            } catch (error) {
               console.error('加载数据失败:', error);
               uni.showToast({
                  title: '加载失败',
                  icon: 'none'
               });
            } finally {
               this.loading = false;
               this.refreshing = false;
               // uni.stopPullDownRefresh();
            }
         },

         // 下拉刷新
         onRefresh() {
            this.refreshing = true;
            this.page = 1;
            this.noMore = false;
            this.loadData();
         },
         // 跳转到详情页
         handleToSpotDetail(item) {
            uni.navigateTo({
               url: `/pages/parking/detail/index?id=${item.spotId}`
            });
         },
         // 加载更多
         loadMore() {
            if (!this.noMore) {
               this.loadData();
            }
         },

         navigateBack() {
            uni.navigateBack({
               delta: 1
            });
         }
      }
   };
</script>

<style scoped>
   .container {
      display: flex;
      flex-direction: column;
      height: 100vh;
      background-color: #f5f5f5;
   }

   .scroll-view {
      flex: 1;
      height: 100%;
   }

   .list {
      padding: 20rpx;
   }

   .item {
      padding: 20rpx;
      margin-bottom: 20rpx;
      background-color: #fff;
      border-radius: 12rpx;
      box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
   }

   .item-content {
      width: 100%;
   }

   .item-row {
      display: flex;
      margin-bottom: 20rpx;
      padding-bottom: 20rpx;
      border-bottom: 1rpx solid #f5f5f5;
   }

   .item-row:last-child {
      margin-bottom: 0;
      padding-bottom: 0;
      border-bottom: none;
   }

   .item-label {
      width: 160rpx;
      color: #666;
      font-size: 28rpx;
   }

   .item-value {
      flex: 1;
      color: #333;
      font-size: 28rpx;
      font-weight: 500;
   }

   .load-more {
      text-align: center;
      padding: 20rpx;
      font-size: 28rpx;
      color: #999;
   }
</style>